package com.zhuangfei.hpuhelper.view;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lzf.serviceplatform.R;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.hpu.bean.OptionalClass;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.OptionalClassParser;
import com.hpu.parser.ScoreParser;
import com.hpu.service.LoginService;
import com.hpu.service.OptionClassService;
import com.hpu.tools.JsonTools;
import com.hpu.tools.ShareTools;
import com.zhuangfei.generalpage.BaseActivity;
import com.zhuangfei.hpuhelper.tools.ActivityTools;
import com.zhuangfei.hpuhelper.tools.AssetTools;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.smartalert.core.LoadAlert;
import com.zhuangfei.smartalert.core.MessageAlert;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;

public class OptionalClassActivity extends BaseActivity {

	/**
	 * 下拉刷新
	 */
	private ListView listView;

	/**
	 * 列表
	 */
	List<Map<String, String>> items = new ArrayList<>();
	SimpleAdapter adapter;

	/**
	 * 业务逻辑类
	 */
	private OptionClassService optionClassService;
	private LoginService loginService;
	
	private LinearLayout backLayout;

	private LoadAlert loadAlert;
	private MessageAlert messageAlert;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_optional_class);
		ActivityTools.setTransparent(this);
		initView();
		initEvent();
		loadData();
	}

	public void loadData() {
		optionClassService.getOptionalClass();
	}
	
	private void initView() {
		loadAlert = new LoadAlert(this).create();
		messageAlert=new MessageAlert(this).create();
		
		backLayout = (LinearLayout) findViewById(R.id.id_back);
		loginService = new LoginService(this,handler);
		optionClassService = new OptionClassService(handler, this);

		// 下拉刷新初始化
		listView = (ListView) findViewById(R.id.id_class_listview);
		
		// 设置适配器
		adapter = new SimpleAdapter(this, items, R.layout.item_score_layout, 
				new String[] { "subject", "type" },
				new int[] { R.id.id_score_name, R.id.id_score_value });
		listView.setAdapter(adapter);

		
	}

	private void initEvent() {
		backLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				ActivityTools.toBackActivityAnim(OptionalClassActivity.this, MainActivity.class);
			}
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
				Map map=items.get(arg2);
				String content="课程名:"+map.get("subject")+"\n课程号:"+map.get("number")
				+"\n类型:"+map.get("type");
				messageAlert.setTitle("成绩详情")
				.setContent(content)
				.show();
			}
			
		});
	}
	
	
	protected void showClass(List<OptionalClass> optionalClasses) {
		items.clear();
		if(optionalClasses.size()==0) Toast.makeText(this, "没有任选课信息", Toast.LENGTH_SHORT).show();
		
		for(int i=0;i<optionalClasses.size();i++){
			OptionalClass optionalClass=optionalClasses.get(i);
			Map<String, String> map = new HashMap<>();
			map.put("subject", optionalClass.getSubject());
			map.put("type", optionalClass.getClassType()+"类");
			map.put("number", optionalClass.getNumber());
			items.add(map);
		}
		
		adapter.notifyDataSetChanged();
	}


	int preWhat=-100;
	int preCount=0;

	DefaultHandler handler = new DefaultHandler(this) {
		public void handleMessage(Message msg) {
			getViewHelper().showSuccessView();
			super.handleMessage(msg);
			if(msg.what==HttpConstants.VPN_TOOLONG){
				if(preWhat==HttpConstants.VPN_TOOLONG){
					preCount++;
				}else{
					preCount=1;
				}
			}else{
				preCount=0;
			}
			preWhat=msg.what;

			switch (msg.what) {
			case HttpConstants.QUERY_CLASS_SUCCESS:
				OptionalClassParser optionalClassParser=new OptionalClassParser();
				List<OptionalClass> optionalClasses=optionalClassParser.parse(msg.obj.toString());
				showClass(optionalClasses);
				break;

			case HttpConstants.VPN_TOOLONG:
				if(preCount>2){
					break;
				}

				getViewHelper().showLoadingView();
				loginService.login(ShareTools.get(OptionalClassActivity.this, ShareConstants.STORE_USER, "").toString(),
						ShareTools.get(OptionalClassActivity.this, ShareConstants.STORE_PASSWORD1, "").toString(),
						ShareTools.get(OptionalClassActivity.this, ShareConstants.STORE_PASSWORD2, "").toString());
				break;

			// 登录成功
			case HttpConstants.LOGIN_SUCCESS:
				getViewHelper().showLoadingView();
				ShareTools.put(OptionalClassActivity.this, ShareConstants.STORE_LOGINCOOKIE, (String) msg.obj);
				optionClassService.getOptionalClass();
				break;
			}
		};
	};
	
	public void onBackPressed() {
		ActivityTools.toBackActivityAnim(this, MenuActivity.class);
	}

}
